package com.qiwx.pigeon.activity.account;

import android.content.Intent;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.qiwx.pigeon.R;
import com.qiwx.pigeon.Utils.MyToast;
import com.qiwx.pigeon.Utils.SharedPreferencesUtil;
import com.qiwx.pigeon.Utils.StringUtil;
import com.qiwx.pigeon.activity.MainActivity;
import com.qiwx.pigeon.base.BaseMVPActivity;
import com.qiwx.pigeon.data.params.LoginParam;
import com.qiwx.pigeon.data.vo.EmployBean;
import com.qiwx.pigeon.presenter.LoginPresenter;
import com.qiwx.pigeon.viewinterface.ILoginView;
import com.qiwx.pigeon.widget.ArrowView;
import com.qiwx.pigeon.widget.ClearableEditText;

import java.lang.ref.WeakReference;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @description
 * @Author qiwx
 * @Date 2020-05-16 09:24
 **/
public class LoginActivity extends BaseMVPActivity<ILoginView, LoginPresenter> implements ILoginView {
    @BindView(R.id.title_content)
    TextView titleContent;
    @BindView(R.id.title_return)
    ArrowView titleReturn;
    @BindView(R.id.title_right)
    TextView titleRight;
    @BindView(R.id.edt_name)
    ClearableEditText edtName;
    @BindView(R.id.edt_pwd)
    ClearableEditText edtPwd;
    @BindView(R.id.show_pwd)
    ImageView showPwd;
    @BindView(R.id.btn_login)
    Button btnLogin;
    LoginParam param = new LoginParam();
    private boolean isShowPwd = true;

    @Override
    public int getLayoutId() {
        return R.layout.activity_login_layout;
    }

    @Override
    public void initAfterOnCreate() {
        titleReturn.changeArrowLeft();
        titleContent.setText("登录");
        String[] s1 = SharedPreferencesUtil.getLoginInfo(this);
        if (s1 != null && s1.length >= 2) {
            edtName.setText(s1[0]);
            edtPwd.setText(s1[1]);
        }

    }


    @Override
    public LoginPresenter createPresenter() {
        return new LoginPresenter(new WeakReference(this));
    }

    public void goMain() {
        Intent go = new Intent(this, MainActivity.class);
        startActivity(go);
    }

    @OnClick({R.id.title_return, R.id.btn_login, R.id.show_pwd})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_return:
                break;
            case R.id.btn_login:
                if (checkParam()) {
                    param.setLogin_name(edtName.getText().toString());
                    param.setPwd(edtPwd.getText().toString());
                    mPresenter.login(param, true);
                }
                break;
            case R.id.show_pwd:
                handlePwdVisible();
                break;
        }
    }

    private void handlePwdVisible() {
        if (isShowPwd) {
            edtPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
            showPwd.setImageResource(R.drawable.pwd_tip_normal);

        } else {
            edtPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
            showPwd.setImageResource(R.drawable.pwd_tip_press);
        }
        edtPwd.setSelection(edtPwd.getText().toString().length());//将光标移至文字末尾
        isShowPwd = !isShowPwd;
        edtPwd.postInvalidate();
    }

    public boolean checkParam() {
        String name = edtName.getText().toString();
        String pwd = edtPwd.getText().toString();
        if (StringUtil.isEmpty(name)) {
            MyToast.showShort("用户名不能为空");
            return false;
        }
        if (StringUtil.isEmpty(pwd)) {
            MyToast.showShort("密码不能为空");
            return false;
        }
        return true;
    }

    @Override
    public void onSuccess(int type) {

    }

    @Override
    public void onLoginSuccess(EmployBean user) {

        SharedPreferencesUtil.setEmployBean(this, user);
        SharedPreferencesUtil.setLoginInfo(this, edtName.getText().toString(), edtPwd.getText().toString());
        goMain();
        finish();
    }

    @Override
    public void onFailure(String error) {


    }
}
